package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.SoftTagMap;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2015/7/10.
 */
@Service
public class TagSoftMapService extends BaseServiceImpl<SoftTagMap> {

    @Autowired
    SystemLogService systemLogService;

    public boolean saveSoftMap(SoftTagMap softMap) {
        Integer currTime = DateUtil.getCurrentTime();
        softMap.setLastModifyTime(currTime);
        SoftTagMap topicSoftMap = getSoftMapBySoftId(softMap.getSoftId());
        if(topicSoftMap==null){
            softMap.setState(0);
            //新增
            insert(softMap);
            systemLogService.log(SystemLog.ModuleTypeEnum.TAGMAPLIST, SystemLog.OperateTypeEnum.ADD, softMap.getId());

        }else{
            SoftTagMap softTagMap = get(softMap.getId());
            softMap.setState(softTagMap.getState());
            update(softMap);

            systemLogService.log(SystemLog.ModuleTypeEnum.TAGMAPLIST, SystemLog.OperateTypeEnum.EDIT, softMap.getId());

        }
        return true;
    }

    public SoftTagMap getSoftMapBySoftId(Integer softId){
        Query<SoftTagMap> tagSoftMapQuery = Query.build(SoftTagMap.class);
        tagSoftMapQuery.addEq(SoftTagMap.COLUMN_SOFTID, softId);
        return get(tagSoftMapQuery);
    }

    //TODO
    public List<SoftTagMap> listSoftTagMaps(Integer tagId) {
        String sql = "SELECT a.*,b.soft_name as soft_name FROM nb_soft_tag_map a ,nb_soft b WHERE a.soft_id = b.soft_id " +
                " AND a.tag_id=" +tagId +
                " ORDER BY a.id asc";
        List<SoftTagMap> topicSoftMaps = findBySql(sql, SoftTagMap.class);
        return topicSoftMaps;
    }

    public boolean deleteSoftMap(Integer id) {
        SoftTagMap domain = get(id);
        domain.setState(SoftTagMap.DELETE);
        Integer curr = DateUtil.getCurrentTime();
        domain.setLastModifyTime(curr);
        update(domain);
        systemLogService.log(SystemLog.ModuleTypeEnum.TAGMAPLIST, SystemLog.OperateTypeEnum.DEL, id);

        return true;
    }
}
